package com.uicole.push.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.uicole.push.server.model.PushMsg;
import com.uicole.push.server.model.Result;
import com.uicole.push.server.service.PushMsgService;

@RestController
public class PushMsgController {
	@Autowired
	private PushMsgService pushMsgService;

	@RequestMapping(value = "pushMsg", method = RequestMethod.POST)
	public Result usersCreate(@ModelAttribute("pushMsg") PushMsg pushMsg,
			BindingResult result) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String ip;
		try {
			ip = getIpAddr(request);
		} catch (Exception e) {
			ip="localhost";
		}
		return pushMsgService.trySave(pushMsg, ip);
	}

	public String getIpAddr(HttpServletRequest request) throws Exception {
		String ip = request.getHeader("x-forwarded-for"); 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("WL-Proxy-Client-IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_CLIENT_IP"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
	    } 
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	      ip = request.getRemoteAddr(); 
	    } 
	    return ip; 
	}
	
	// PushMsgs
		@RequestMapping(value = "/pushMsgs", method = RequestMethod.GET)
		public String pushMsgsList(Model model) {
			model.addAttribute("pushMsgs", pushMsgService.getAll());
			return "pushMsg/pushMsgs_index";
		}

		@RequestMapping(value = "/pushMsgs/new", method = RequestMethod.GET)
		public String pushMsgsNew(Model model) {
			model.addAttribute("pushMsg", new PushMsg());
			return "pushMsg/pushMsgs_create";
		}

		@RequestMapping(value = "/pushMsgs", method = RequestMethod.POST)
		public String pushMsgsCreate(Model model, PushMsg pushMsg) {
			PushMsg existing = pushMsgService.get(pushMsg.getId());
			if (existing != null) {
				model.addAttribute("error", "PushMsg already exist !");
				model.addAttribute("pushMsgs", pushMsgService.getAll());
				return "pushMsg/pushMsgs_index";
			}
			pushMsgService.save(pushMsg);
			model.addAttribute("info", "PushMsg created successfully");
			return "pushMsg/pushMsgs_get";
		}

		@RequestMapping(value = "/pushMsgs/{id}", method = RequestMethod.GET)
		public String pushMsgsRetrieve(@PathVariable Long id, Model model) {
			PushMsg pushMsg = pushMsgService.get(id);
			model.addAttribute("pushMsg", pushMsg);
			return "pushMsg/pushMsgs_get";
		}

		@RequestMapping(value = "/pushMsgs/edit/{id}", method = RequestMethod.GET)
		public String pushMsgsEdit(@PathVariable Long id, Model model) {
			PushMsg pushMsg = pushMsgService.get(id);
			model.addAttribute("pushMsg", pushMsg);
			return "pushMsg/pushMsgs_update";
		}

		@RequestMapping(value = "/pushMsgs/update", method = RequestMethod.POST)
		public String pushMsgsUpdate(Model model, PushMsg pushMsg) {
			PushMsg oldPushMsg = pushMsgService.get(pushMsg.getId());
			
			model.addAttribute("info", "PushMsg updated successfully");
			model.addAttribute("pushMsg", oldPushMsg);
			return "pushMsg/pushMsgs_get";
		}

		@RequestMapping(value = "/pushMsgs/delete/{id}", method = { RequestMethod.GET })
		public String pushMsgsDelete(@PathVariable Long id, Model model) {
			pushMsgService.remove(id);
			model.addAttribute("info", "PushMsg deleted successfully");
			model.addAttribute("pushMsgs", pushMsgService.getAll());
			return "pushMsg/pushMsgs_index";
		}

		@RequestMapping(value = "/pushMsgs/search", method = RequestMethod.POST)
		public String pushMsgsSearch(Model model, PushMsg pushMsg) {
			List<PushMsg> pushMsgs = pushMsgService.getAll();
			model.addAttribute("pushMsgs", pushMsgs);
			model.addAttribute("search", true);
			return "pushMsg/pushMsgs_index";
		}
}
